package com.example.mask.maskmobilesshop.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.example.mask.maskmobilesshop.entity.Attribute;
import com.example.mask.maskmobilesshop.mappers.AttritubeMapper;
import com.example.mask.maskmobilesshop.service.AttritubeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//属性
@Service
public class IAttritubeService extends ServiceImpl<AttritubeMapper, Attribute> implements AttritubeService {


    @Autowired
    private AttritubeMapper attritubeMapper;

    @Override
    public List<Map<String, Object>> findByType(Integer type) throws Exception {
        try {
            List<Map<String, Object>> result = attritubeMapper.selectByType(type);
            List<Map<String, Object>> attributes = new ArrayList<Map<String, Object>>();

            for (int i = 0; i < result.size(); i++) {
                Map<String, Object> attributeName = new HashMap<String, Object>();
                Object attrNameId = result.get(i).get("attrNameId");
                attributeName.put("attrNameId", result.get(i).get("attrNameId"));
                attributeName.put("attributeName", result.get(i).get("attributeName"));

                List<Map<String, Object>> attributeValues = new ArrayList<Map<String, Object>>();
                for (int j = 0; j < result.size(); j++) {
                    if (attrNameId.equals(result.get(j).get("attrNameId"))) {
                        Map<String, Object> attributeValue = new HashMap<String, Object>();
                        attributeValue.put("attrValId", result.get(j).get("attrValId"));
                        attributeValue.put("attributeValue", result.get(j).get("attributeValue"));
                        if (!attributeValues.contains(attributeValue)) {
                            attributeValues.add(attributeValue);
                        }
                    }
                }
                attributeName.put("attributeValues", attributeValues);
                if (!attributes.contains(attributeName)) {
                    attributes.add(attributeName);
                }
            }

            return attributes;
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        }
    }
}
